// signin.js
// Implement 'stay signed in'.

function StaySignedIn(changeInfo)
{
	if(changeInfo.removed)
		return;
	var cookie = changeInfo.cookie;
	var a_year = 365 * 24 * 60 * 60;
	var now = (new Date()).getTime()/1000;
	if(cookie.name == "56f61_winduser" && 
		cookie.expirationDate - now < a_year)
		UpdateCookie({expirationDate: now + a_year * 10}, cookie);
}

//The following code works if and only if onChanged won't be fired unless 
//the value of StaySignedIn REALLY changes and calling removeListener which 
//the event doesn't have won't raise an error.
Options.SignIn.onChanged = (function()
{
	if(Options.SignIn.StaySignedIn)
		chrome.cookies.onChanged.addListener(StaySignedIn);
	else
		chrome.cookies.onChanged.removeListener(StaySignedIn);
});
Options.SignIn.onChanged();

var Sessions = 
{
	GetCurrent: (function()
	{
		var sessions = Options.SignIn.SavedSessions;
		for(var id in sessions)
			if(sessions[id].Current)
				return sessions[id];
	}), 

	GetCurrentId: (function()
	{
		var sessions = Options.SignIn.SavedSessions;
		for(var id in sessions)
			if(sessions[id].Current)
				return id;
	}), 

	SaveCurrent: (function(callback)
	{
		var sessions = Options.SignIn.SavedSessions;
		var id = this.GetCurrentId();
		chrome.cookies.getAll({}, function(cookies)
		{
			cookies.forEach(function(cur){
				delete cur.storeId;    });
			sessions[id].Cookies = cookies;
			Options.SignIn.SavedSessions = sessions;
			callback && callback();
		});
	}), 

	SetCurrent: (function(id, callback)
	{
		var sessions = Options.SignIn.SavedSessions;
		sessions[this.GetCurrentId()].Current = false;
		sessions[id].Current = true;
		var newCookies = sessions[id].Cookies;
		Sessions.itemsLeft["SetCurrent"] = newCookies.length;
		if(newCookies.length == 0)
			callback && callback();
		newCookies.forEach(function(cur)
		{
			UpdateCookie({}, cur, function(cookie)
			{
				if(!cookie)
					callback && callback(
						{error: chrome.extension.lastError});
				else if(--Sessions.itemsLeft["SetCurrent"] == 0)
					callback && callback();
			});
		});
		Options.SignIn.SavedSessions = sessions;
	}), 

	ClearCookies: (function(callback)
	{
		chrome.cookies.getAll({}, function(cookies)
		{
			Sessions.itemsLeft["ClearCookies"] = cookies.length;
			if(cookies.length == 0)
				callback && callback();
			cookies.forEach(function(cur)
			{
				chrome.cookies.remove({url: GetURLFromCookie(cur), 
					name: cur.name}, function(details)
					{
						if(!details)
							callback && callback(
								{error: chrome.extension.lastError});
						else if(--Sessions.itemsLeft["ClearCookies"] == 0)
							callback && callback();
					});
			});
		});
	}), 

	AddSession: (function(callback)
	{
		var sessions = Options.SignIn.SavedSessions;
		var i = 1;
		var id;
		while(sessions[id = "未登录会话 " + i.toString()])
			++i;
		sessions[id] = ({Cookies: []});
		Options.SignIn.SavedSessions = sessions;
		Sessions.SwitchSession(id, callback);
	}), 

	RemoveSession: (function(id)
	{
		if(Sessions.GetCurrentId() == id)
			return; //Refuse to remove current session
		var sessions = Options.SignIn.SavedSessions;
		delete sessions[id];
		Options.SignIn.SavedSessions = sessions;
	}), 

	SwitchSession: (function(id, callback)
	{
		if(Sessions.GetCurrentId() == id)
			return;
		Sessions.SaveCurrent(function()
		{
			Sessions.ClearCookies(function(result)
			{
				if(result)
					callback && callback(result);
				else
					Sessions.SetCurrent(id, function(result)
					{
						if(result)
							callback && callback(result);
						else
						{
							Sessions.onSwitched();
							callback && callback();
						}
					});
			});
		});
	}), 

	onSwitched : (function()
	{
		GetAllBBSTabs(function(tabs)
		{
			tabs.forEach(function(cur)
			{
				(chrome.tabs.sendMessage || chrome.tabs.sendRequest)(
					cur.id, {name: "sessionSwitched"});
			});
		});
	}), 

	itemsLeft : {}, 
};

